package com.yanqu.road.server.cmd.activity.stronghold.cross;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.stronghold.StrongholdPatronsData;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.strongholdwar.StrongholdModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdActivityMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = GameProtocol.S_CROSS_STRONGHOLD_WILD_DISPATCH, desc = "跨服返回派遣结果")
public class CrossDispatchWildCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {

        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        StrongholdWarProto.DispatchPatronsToCrossWildRespMsg req = StrongholdWarProto.DispatchPatronsToCrossWildRespMsg.parseFrom(packet.getBytes());
        ret = req.getRet();
        int patronsId = req.getPatronsId();
        ActivityInfo activityInfo = StrongholdActivityMgr.getOpenActivityInfo();
        StrongholdPatronsData data = player.getModule(StrongholdModule.class).getChallengePatronsData(activityInfo.getActivityId(), patronsId);
        String consume = null;
        if (data != null && data.getFreeTimes() >= StrongholdActivityMgr.getPatronsFreeTimes(player, patronsId)) {
            consume = StrongholdActivityMgr.getWildDispatchConsume(data.getBuyTimes());
        }
        if (consume != null) {
            player.getModule(PreReduceModule.class).restorePreReduce(consume);
        }
        if (ret == 0) {
            if (consume != null) {
                player.getModule(CurrencyModule.class).removeCurrency(PropertyHelper.parseStringToProperty(consume), eLogMoneyType.Stronghold, eLogMoneyType.StrongholdWildDispatch);
            }
            player.getModule(StrongholdModule.class).addBattlePatrons(patronsId);
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        StrongholdWarProto.DispatcherPatronsRespMsg.Builder resp = StrongholdWarProto.DispatcherPatronsRespMsg.newBuilder();
        resp.setRet(ret);
        player.sendPacket(ClientProtocol.U_STRONGHOLD_WAR_SET_WILD_PATRONS, resp);
    }
}
